// SPDX-License-Identifier: GPL-2.0+

#include <common.h>
#include <usb.h>
#include <led.h>
#include <asm/io.h>

#include <mach/loongson.h>

#include "platform.h"

int ls2x_set_usbphy(void)
{
	u32 val = 0;

#ifdef CONFIG_USB_XHCI_DWC3
	/*init dwc3*/
	//usb3.0 use internal reference clk
	val = readl((unsigned int *)PHYS_TO_UNCACHED(0x1f06ff00));
	val = val & ~(1 << 2);
	writel(val, (unsigned int *)PHYS_TO_UNCACHED(0x1f06ff00));

	val = readl((unsigned int *)PHYS_TO_UNCACHED(0x1f06ff04));
	val = val | 0x00040000;
	writel(val, (unsigned int *)PHYS_TO_UNCACHED(0x1f06ff04));
	val = readl((unsigned int *)PHYS_TO_UNCACHED(0x1f060000 + 0xc110));
	val = val & ~(3 << 12);
	val = val | (1 << 12);
	writel(val, (unsigned int *)PHYS_TO_UNCACHED(0x1f060000 + 0xc110));
	val = readl((unsigned int *)PHYS_TO_UNCACHED(0x1f060000 + 0xc2c0));
	val = val & ~(3 << 1);
	val = val | (0 << 1);
	writel(val, (unsigned int *)PHYS_TO_UNCACHED(0x1f060000 + 0xc2c0));
#endif
	val = readl((unsigned int *)PHYS_TO_UNCACHED(0x1fe10104));
	val = val & ~(1 << 5);//关闭预读取
	val = val | (1 << 1);//usb otg模式
	val = val | (1 << 0);//usb cache一致性
	writel(val, (unsigned int *)PHYS_TO_UNCACHED(0x1fe10104));

	return 0;
}
